# coding=utf-8

"""
用户模块
"""

from django.db import models
from django.contrib.auth.models import BaseUserManager
from django.contrib.auth.models import AbstractBaseUser


class UserManager(BaseUserManager):

    def create_user(self, name, password=None):

        user = self.model(name=name)
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, name, password=None):

        user = self.create_user(name, password)
        user.is_admin = True
        user.save(using=self._db)
        return user


class User(AbstractBaseUser):
    """用户表"""

    GENDER_CHOICES = (
        (1, "男"),
        (2, "女"),
        (3, "未知"),
    )

    USER_STATUS = (
        (0, "正常状态"),
        (-1, "已删除"),
    )

    name = models.CharField(max_length=32, default="", verbose_name="用户名", unique=True)
    mobile = models.CharField(max_length=32, default="", verbose_name="电话号码", blank=True)
    token = models.CharField(max_length=64, verbose_name="OpenID", default="")

    create_time = models.DateTimeField(auto_now=False, auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="修改时间")

    # 是否被删除 & 是否激活 & 是否是管理员
    is_delete = models.BooleanField(default=False, verbose_name=u"是否被删除")
    is_active = models.BooleanField(default=True, verbose_name=u"是否激活")
    is_admin = models.BooleanField(default=False, verbose_name=u"是否是管理员")

    objects = UserManager()

    USERNAME_FIELD = "name"
    #REQUIRED_FIELDS = ("name", )

    class Meta:
        ordering = ("-create_time",)
        verbose_name = "用户管理"
        verbose_name_plural = "用户管理"

    def get_full_name(self):
        return self.name

    def get_short_name(self):
        return self.name

    def has_perm(self, perm, obj=None):
        return True

    def has_module_perms(self, app_label):
        return True

    @property
    def is_superuser(self):
        return True

    @property
    def is_staff(self):
        return self.is_admin

    def __str__(self):
        return self.name
